#cc0 license
#credit :mn,richitaroy,ankitaroy

from cbCompilation import *


def main():
    file = open("first.txt", "r")
    outfile = open("first.txt.py", "w") # writing a file
    lines = file.readlines()
    file.close()
    arr = list()
    outfile.write("from cbCompilation import* \r\n") # importing cb functions

    for line in lines:
        line = line.strip().lower()
        arr.append(line)

    forwardonly = -1 # keeps count of the lines interpreted

    for i in range(len(arr)):
        if i > forwardonly:
            forwardonly = i



            if cbright(arr[i],5) == "print" and cbleft(arr[i],2) != "cb": #for print statements which donot start with "cb"
                quot = chr(34)
                lastindex = arr[i].rindex('p')
                printwhat = (quot + arr[i][0:lastindex] + quot)
                p = """print(""" + printwhat + """)"""
                outfile.write(p + "\r\n")

            if cbleft(arr[i], 2) == "cb" and cbright(arr[i], 5) != "print": # for cbdefs
                outfile.write(arr[i])

            if cbleft(arr[i], 2) == "cb" and cbright(arr[i], 5) == "print": #printing out cbdefs
                lastindex = arr[i].rindex(')')
                command = """print(""" + arr[i][0:lastindex + 1] + """)"""
                outfile.write(command + "\r\n")

            if cbleft(arr[i], 5) == "while" and cbright(arr[i], 1) == ":":  # while loop
                outfile.write("while True:" + "\r\n")
                for a in range(i + 1, len(arr)):
                    if a > forwardonly:
                        forwardonly = a
                    if arr[a] != "end":
                        outfile.write(all_loop(arr[a]) + "\r\n")
                    else:
                        outfile.write(stringindent("break", 4) + "\r\n")
                        break

            if cbleft(arr[i], 3) == "for" and cbright(arr[i], 1) == ":":  #for loop
                rang = arr[i].index("(")
                outfile.write("for i in range" + arr[i][rang:] + "\r\n")
                for a in range(i + 1, len(arr)):
                    if a > forwardonly:
                        forwardonly = a
                    if arr[a] != "end":
                        outfile.write(all_loop(arr[a]) + "\r\n")
                    else:
                        break
            if arr[i].find("=") != -1 and cbleft(arr[i],2) != "cb" and cbleft(arr[i],2) != "if" and cbleft(arr[i],4) != "elif" and cbleft(arr[i],2) != "else" and cbright(arr[i],3)!= "int":#  for "equal to" assignment operator
                equallist = list()
                for p in arr[i].split("="):
                    equallist.append(p)
                outfile.write(equallist[0]+ "=" + equallist[1]+ "\r\n")


            if cbleft(arr[i], 2) == "if" and cbright(arr[i], 1) == ":": #if loop
                rang = arr[i].index("(")
                outfile.write("if " + arr[i][rang:] + "\r\n")
                for a in range(i + 1, len(arr)):
                    if a > forwardonly:
                        forwardonly = a
                    if arr[a] != "end":
                        outfile.write(str(all_loop(arr[a])) + "\r\n")
                    else:
                        break
            if cbleft(arr[i], 4) == "elif" and cbright(arr[i], 1) == ":": #elif loop
                rang = arr[i].index("(")
                outfile.write("elif " + arr[i][rang:] + "\r\n")
                for a in range(i + 1, len(arr)):
                    if a > forwardonly:
                        forwardonly = a
                    if arr[a] != "end":
                        outfile.write(str(all_loop(arr[a])) + "\r\n")
                    else:
                        break
            if cbleft(arr[i], 4) == "else" and cbright(arr[i], 1) == ":": #else loop
                outfile.write("else :"  + "\r\n")
                for a in range(i + 1, len(arr)):
                    if a > forwardonly:
                        forwardonly = a
                    if arr[a] != "end":
                        outfile.write(str(all_loop(arr[a])) + "\r\n")
                    else:
                        break
            if cbleft(arr[i],1) =="#": #for commented out lines
                forwardonly += 1
            if cbright(arr[i], 3) == "int" and cbright(arr[i], 5) != "print" and arr[i].find("=") != -1:
                # explicit datatype conversion for int
                command = str(analyze_convert(arr[i])) + "\r\n"
                outfile.write(command)

def all_loop(x):  #  Used for statements inside loop
    if cbleft(x, 2) == "cb" and cbright(x, 5) != "print":
        return stringindent(x, 4)
    if cbleft(x, 2) == "cb" and cbright(x, 5) == "print":
        lastindex = x.rindex(')')
        command = """print(""" + x[0:lastindex + 1] + """)"""
        return stringindent(command, 4)
    if x.find("=") != -1 and cbleft(x, 2) != "cb":
        equallist = list()
        for p in x.split("="):
            equallist.append(p)
        command = equallist[0] + "=" + equallist[1]
        return stringindent(command,4)
    if cbright(x, 5) == "print" and cbleft(x, 2) != "cb":
        quot = chr(34)
        lastindex = x.rindex('p')
        printwhat = quot + x[0:lastindex] +quot
        p = """print(""" + printwhat + """)"""
        return stringindent(p,4)
    if cbright(x, 3) == "int" and cbright(x, 5) != "print" and x.find("=") != -1:
        command = str(analyze_convert(x)) + "\r\n"
        return stringindent(command,4)


def stringindent(x, lev):  #  for indentation
    return chr(32) * lev + x

def analyze_convert(x): #for converting datatype to int
    parts = list()
    for p in x.split("="):
        parts.append(p)
    last_index = parts[1].rindex("i")
    extract = parts[1][0:last_index]
    command = parts[0] + "=" + "int(" + extract +")"
    return command

main()





